//
//  Dire_View.m
//  MagicBox
//
//  Created by dllo on 16/8/15.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "Dire_View.h"
#import "UIView+Add.h"
#import "UIControl+Add.h"
#import "LFLiveSession.h"

@interface Dire_View()<LFLiveSessionDelegate>



@property (nonatomic, retain) UIButton *meiyanButton;
@property (nonatomic, retain) UIButton *jingtouButton;
@property (nonatomic, retain) UIButton *guanbiButton;

@property (nonatomic, retain) UIButton *kaishiButton;
@property (nonatomic, retain) UIView *containerView;
@property (nonatomic, retain) LFLiveDebug *debugInfo;
@property (nonatomic, retain) LFLiveSession *session;
@end
@implementation Dire_View

-(instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    if (self){
        [self caseVideo];
        [self creactAVVideo];
        UIImageView *imageview = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"zhanweitu.png"]];
        imageview.frame = self.bounds;
        [self addSubview:imageview];
        
        [self addSubview:self.containerView];
        
        //美颜
        [self.containerView addSubview:self.meiyanButton];
        //切换前后摄像头
        [self.containerView addSubview:self.jingtouButton];
        //关闭直播
        [self.containerView addSubview:self.guanbiButton];
        //开始直播
        [self.containerView addSubview: self.kaishiButton];
        
        
    }
    return self;
    
}
-(void)creactAVVideo{
    __weak typeof (self) _self = self;
    AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    switch (status) {
        case AVAuthorizationStatusNotDetermined:{
            // 许可对话没有出现，发起授权许可
            [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
                if (granted) {
                    dispatch_async(dispatch_get_main_queue(), ^{
                        [_self.session setRunning:YES];
                    });
                }
            }];
            break;
        }
        case AVAuthorizationStatusAuthorized:{
            // 已经开启授权，可继续
            [_self.session setRunning:YES];
            break;
        }
        case AVAuthorizationStatusDenied:
        case AVAuthorizationStatusRestricted:
            // 用户明确地拒绝授权，或者相机设备无法访问
            
            break;
        default:
            break;
            
    }
}
-(void)caseVideo{
    AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];
    switch (status) {
        case AVAuthorizationStatusNotDetermined:{
            [AVCaptureDevice requestAccessForMediaType:AVMediaTypeAudio completionHandler:^(BOOL granted) {
            }];
            break;
        }
        case AVAuthorizationStatusAuthorized:{
            break;
        }
        case AVAuthorizationStatusDenied:
        case AVAuthorizationStatusRestricted:
            break;
        default:
            break;
    }
    
    
}
-(void)liveSession:(LFLiveSession *)session liveStateDidChange:(LFLiveState)state{
    NSLog(@"这是啥?");
}
-(void)liveSession:(LFLiveSession *)session debugInfo:(LFLiveDebug *)debugInfo{
    NSLog(@"这又是啥");
}
-(void)liveSession:(LFLiveSession *)session errorCode:(LFLiveSocketErrorCode)errorCode{
    NSLog(@"这个是错误信息么?");
}

-(LFLiveSession*)session{
    if(!_session){
        _session = [[LFLiveSession alloc] initWithAudioConfiguration:[LFLiveAudioConfiguration defaultConfiguration] videoConfiguration:[LFLiveVideoConfiguration defaultConfiguration]];
        
        _session.running = YES;
        _session.preView = self;
        
    }
    return _session;
}
-(UIView*)containerView{
    if (!_containerView){
        _containerView = [UIView new];
        _containerView.frame = self.bounds;
        //        _containerView.backgroundColor = [UIColor redColor];
        _containerView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        
        
    }
    return _containerView;
}
-(UIButton*)guanbiButton{
    if(!_guanbiButton){
        _guanbiButton = [UIButton new];
        _guanbiButton.frame = CGRectMake(SCREENWIDTH/4*3,SCREENHEIGHT/5, SCREENWIDTH/4, 50);
        [_guanbiButton setTitle:@"关闭页面" forState:UIControlStateNormal];
        
        _guanbiButton.exclusiveTouch = YES;
        __weak __typeof__(self) weakSelf = self;
        [_guanbiButton addBlockForControlEvents:UIControlEventTouchUpInside block:^(id sender) {
            [[weakSelf naviController]popViewControllerAnimated:YES];
            
        }];
        
    }
    return _guanbiButton;
}
-(UIButton*)jingtouButton{
    if(!_jingtouButton){
        _jingtouButton = [UIButton new];
        _jingtouButton.frame = CGRectMake(SCREENWIDTH/4*3, SCREENHEIGHT/4, SCREENWIDTH/4, 50);
        
        [_jingtouButton setTitle:@"切换镜头" forState:UIControlStateNormal];
        _jingtouButton.exclusiveTouch = YES;
        __weak __typeof__(self) _self = self;
        [_jingtouButton addBlockForControlEvents:UIControlEventTouchUpInside block:^(id sender) {
            AVCaptureDevicePosition devicePositon = _self.session.captureDevicePosition;
            _self.session.captureDevicePosition =  (devicePositon == AVCaptureDevicePositionBack) ? AVCaptureDevicePositionFront : AVCaptureDevicePositionBack;
            
        }];
        
        
    }
    
    return _jingtouButton;
    
}
-(UIButton*)meiyanButton{
    if(!_meiyanButton){
        _meiyanButton = [UIButton new];
        _meiyanButton.frame = CGRectMake(SCREENWIDTH/4*3, SCREENHEIGHT/3.5,SCREENWIDTH/4, 50);
        [_meiyanButton setTitle:@"开始美颜" forState:UIControlStateSelected];
        [_meiyanButton setTitle:@"关闭美颜" forState:UIControlStateNormal];
        _meiyanButton.exclusiveTouch = YES;
        __weak typeof(self) _self = self;
        [_meiyanButton addBlockForControlEvents:UIControlEventTouchUpInside block:^(id sender) {
            _self.session.beautyFace = !_self.session.beautyFace;
            _self.meiyanButton.selected = !_self.session.beautyFace;
        }];
    }
    
    return _meiyanButton;
}
-(UIButton*)kaishiButton{
    if(!_kaishiButton){
        _kaishiButton = [UIButton new];
        
        _kaishiButton.frame = CGRectMake(SCREENWIDTH/4*3, SCREENHEIGHT/3,SCREENWIDTH/4, 50);
        _kaishiButton.layer.cornerRadius = _kaishiButton.frame.size.height*0.5;
//        [_kaishiButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//        [_kaishiButton.titleLabel setFont:[UIFont systemFontOfSize:16]];
        [_kaishiButton setTitle:@"开始直播" forState:UIControlStateNormal];
//        [_kaishiButton setBackgroundColor:[UIColor grayColor]];
        _kaishiButton.exclusiveTouch = YES;
        __weak typeof(self) _self = self;
        [_kaishiButton addBlockForControlEvents:UIControlEventTouchUpInside block:^(id sender) {
            _self.kaishiButton.selected = !_self.kaishiButton.selected;
            if(_self.kaishiButton.selected){
                [_self.kaishiButton setTitle:@"结束直播" forState:UIControlStateNormal];
                LFLiveStreamInfo *stream = [LFLiveStreamInfo new];
                stream.url = @"http://pull99.a8.com/live/Gao123.flv?ikHost=ws&ikOp=1&CodecInfo=8192";
                [_self.session startLive:stream];
            }else{
                [_self.kaishiButton setTitle:@"开始直播" forState:UIControlStateNormal];
                [_self.session stopLive];
            }
        }];
    }
    return _kaishiButton;
}




//当前控制器的导航控制器
- (UINavigationController*)naviController {
    
    for (UIView* next = [self superview]; next; next = next.superview) {
        
        UIResponder* nextResponder = [next nextResponder];
        
        if ([nextResponder isKindOfClass:[UINavigationController class]]) {
            
            return (UINavigationController*)nextResponder;
        }
    }
    return nil;
}

@end
